<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-19 11:28:55
// +-----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model;

/**
 * 用户认证模型
 * @author 琥珀 (317559272@qq.com)
 */
Class LoginModel extends Model
{

    protected $tableName = 'admin';

    /**
     * 登录指定用户
     * @param  integer $uid 用户ID
     * @return boolean      ture-登录成功，false-登录失败
     */
    public function login($username, $password) {

        //判断用户是否空
        if (empty($username)) {
            return -1;
        }

        //判断密码是否空
        if (empty($password)) {
            return -2;
        }

        //对比用户
        $map['username'] = $username;
        $user = $this->where($map)->field(true)->find();
        if (!$user || 1 != $user['status']) {
            loginlog($username, $password, '用户名错误');
            return -3;
        }

        //对比密码
        if ($user['password'] !== md5($password)) {
            loginlog($username, $password, '密码错误');
            return -4;
        }

        /* 登录用户 */
        $this->autoLogin($user);
        syslogs('登陆', $_SERVER['REQUEST_URI']);
        return true;
    }

    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    private function autoLogin($user) {

        /* 更新登录信息 */
        $data = array('uid' => $user['uid'], 'login_times' => array('exp', '`login_times`+1'), 'last_login_time' => NOW_TIME, 'online' => '1', 'last_login_ip' => get_client_ip(1), 'session_id' => session_id(),);
        $this->save($data);

        /* 记录登录SESSION和COOKIES */
        $auth = array(
            'uid' => $user['uid'],
            'realname' => $user['realname'],
            'last_login_time' => $user['last_login_time'],
            'session_id' => $user['session_id'],
            'online' => $user['online'],
            'default_hospital_id' => $user['default_hospital_id'],
            'hospital_id' => $user['hospital_id'],
            'login_times' => $user['login_times'],
            'depart_id' => $user['depart_id'],
            'data_id'   => $user['data_id']
            );
        session('user_auth', $auth);
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout() {
        $model = M('Admin');
        $user = session('user_auth');
        $data = array('uid' => $user['uid'], 'last_login_time' => NOW_TIME, 'online' => '0', 'session_id' => null);
        $model->save($data);
        session('user_auth', null);
    }
}
?>